package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.SensitiveMapper;
import com.heima.wemedia.service.SensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, WmSensitive> implements SensitiveService {
    @Override
    public ResponseResult insert(WmSensitive dto) {
        //1.校验
        if(dto==null || StringUtils.isBlank(dto.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断是否存在该敏感词
//        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(WmSensitive::getSensitives, dto.getSensitives());
//        WmSensitive sensitiveDB = super.getOne(wrapper);

        WmSensitive sensitiveDB = super.getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        if(sensitiveDB!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该敏感词已存在");
        }

        //3.新增
        dto.setCreatedTime(new Date());
        super.save(dto);

        //4.返回
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(WmSensitive dto) {
        //1.校验
        if(dto==null || StringUtils.isBlank(dto.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断该敏感词是否已存在
        WmSensitive sensitiveDB = super.getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        if(sensitiveDB!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该敏感词已存在");
        }
        //校验id
        WmSensitive sensitive = super.getById(dto.getId());
        if(sensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //3.修改
        updateById(dto);

        //4.返回
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult del(Integer id) {
        //1.校验
        if(id==null || id<0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断id是否存在
        WmSensitive sensitiveDB = this.getById(id);
        if(sensitiveDB==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //3.存在则删除
        removeById(id);

        //4.返回
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.校验
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.检查分页条件
        dto.checkParam();

        //3.分页查询db
        IPage<WmSensitive> pageParam = new Page<>(dto.getPage(), dto.getSize());//分页参数
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(WmSensitive::getSensitives, dto.getName());
        }
        IPage<WmSensitive> page = this.page(pageParam, wrapper);


        //4.封装结果返回
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int)page.getTotal());
        result.setData(page.getRecords());
        return result;
    }
}
